package sunsun.xiaoli.jiarebang.device.pondteam

import `in`.srain.cube.views.ptr.PtrFrameLayout
import `in`.srain.cube.views.ptr.PtrHandler2
import android.os.Bundle
import android.view.View
import android.widget.AbsListView
import android.widget.ImageView
import android.widget.TextView
import com.itboye.pondteam.base.BaseActivity
import com.itboye.pondteam.bean.PondCleanRecords
import com.itboye.pondteam.custom.ptr.BasePtr
import com.itboye.pondteam.presenter.UserPresenter
import com.itboye.pondteam.utils.LogUtils
import com.itboye.pondteam.utils.loadingutil.MAlert
import kotlinx.android.synthetic.main.activity_pond_clean_log.*
import sunsun.xiaoli.jiarebang.R
import sunsun.xiaoli.jiarebang.adapter.PondCleanLogsAdapter
import sunsun.xiaoli.jiarebang.app.App
import sunsun.xiaoli.jiarebang.utils.ComparePondFilterVersion
import sunsun.xiaoli.jiarebang.utils.ComparePondFilterVersion.changeHostOrPort
import java.util.*
import kotlin.collections.ArrayList

class PondCleanLogActivity : BaseActivity(), Observer {

    var img_back: ImageView? = null
    var txt_title: TextView? = null
    var ptrFrame: PtrFrameLayout? = null
    var userPresenter: UserPresenter = UserPresenter(this)
    internal var app: App? = null

    var did = ""
    var pageIndex = 1
    internal var isAtTop = true
    internal var isAtBottom = false
    var pondCleanLogArrayList = ArrayList<PondCleanRecords>()

    var pondCleanAdapter: PondCleanLogsAdapter? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_pond_clean_log)
        app = application as App?
        txt_title!!.text = intent.getStringExtra("title")
        initView()
        initIntentData()
        initData()
    }

    private fun initData() {

        pondCleanAdapter = PondCleanLogsAdapter(pondCleanLogArrayList, this)
        pull_refresh_lv.adapter = pondCleanAdapter


        queryCleanLogsList()
    }

    private fun initView() {
        BasePtr.setPagedPtrStyle(ptrFrame)
        ptrFrame!!.setPtrHandler(object : PtrHandler2 {
            override fun onRefreshBegin(frame: PtrFrameLayout?) {
                pageIndex = 1
                queryCleanLogsList()
            }

            override fun onLoadMoreBegin(frame: PtrFrameLayout?) {
                pageIndex++
                queryCleanLogsList()

            }

            override fun checkCanDoRefresh(frame: PtrFrameLayout?, content: View?, header: View?): Boolean {
                return isAtTop
            }

            override fun checkCanDoLoadMore(frame: PtrFrameLayout?, content: View?, footer: View?): Boolean {
                return isAtBottom
            }


        })
        pull_refresh_lv!!.setOnScrollListener(object : AbsListView.OnScrollListener {
            override fun onScroll(p0: AbsListView?, p1: Int, p2: Int, p3: Int) {
                if (p1 == 0) {
                    var firstVisibleItemView = pull_refresh_lv.getChildAt(0)
                    if (firstVisibleItemView != null && firstVisibleItemView.getTop() == 0) {
                        LogUtils.v("ListView", "##### 滚动到顶部 #####")
                        isAtTop = true
                        isAtBottom = false
                    } else {
                        isAtTop = false
                    }
                } else if ((p1 + p2) == p3) {
                    var lastVisibleItemView = pull_refresh_lv.getChildAt(pull_refresh_lv.getChildCount() - 1);
                    if (lastVisibleItemView != null && lastVisibleItemView.getBottom() == pull_refresh_lv.getHeight()) {
                        LogUtils.v("ListView", "##### 滚动到底部 ######")
                        isAtBottom = true
                        isAtTop = false
                    } else {
                        isAtBottom = false
                    }
                }
            }

            override fun onScrollStateChanged(p0: AbsListView?, p1: Int) {
            }

        })
    }

    private fun initIntentData() {
        did = intent.getStringExtra("did")
        changeHostOrPort(app!!.pondDeviceDetailUI.deviceDetailModel.getVer(), userPresenter, null, ComparePondFilterVersion.isTrue(app!!.pondDeviceDetailUI.deviceDetailModel.getVer()))
    }

    private fun queryCleanLogsList() {
        showProgressDialog(getStringValue(R.string.requesting), true)
        userPresenter.pondCleanRecords(did, pageIndex)
    }

    override fun onClick(p0: View?) {
        when (p0!!.id) {
            R.id.img_back -> {
                finish()
            }
            else -> {
            }
        }
    }


    override fun update(p0: Observable?, data: Any?) {
        var entity = handlerError(data)
        closeProgressDialog()
        ptrFrame!!.refreshComplete()
        if (entity != null) {
            if (entity.code != 0) {
                MAlert.alert(entity.msg)
            } else {
                if (entity.eventType == UserPresenter.pondCleanRecords_success) {
                    var pondArray = entity.data as ArrayList<PondCleanRecords>//得到清洗日志
                    if (pondArray != null) {
                        if (pageIndex == 1) {
                            pondCleanLogArrayList.clear()
                        }
                        pondCleanLogArrayList.addAll(pondArray)
                        pondCleanAdapter!!.notifyDataSetChanged()
                    }
                } else if (entity.eventType == UserPresenter.pondCleanRecords_fail) {
                    MAlert.alert(entity.msg)
                }
            }
        }
    }

}
